function [msg_cycle_finished, alg_finished] = mobile_msg_cycle_step( alg_param )
    
    global nodes_count;
    global spin_start_node;     % sensor, który próbuje nadać wiadomość
    global S_Map;             % Sensors map (see put_sensors.m)
    global D_Map;             % Distances map (see put_sensor.m)
    global N_Map;             % Neighbours map (see neighbouring.m)
    global msg_cycle_finished;
    global N_Range;
    global random_color;
    global counter;
    global Random_Node_List;
    global f1;
    
    counter = counter + 1 ;
    alg_finished = true;
    
    Random_Node_List = randperm(nodes_count);
    
    rand('state',sum(100*clock));
    random_color = rand(1,3);
    
    draw_circle(S_Map(spin_start_node,1), S_Map(spin_start_node,2), 3,100);
    plot3(S_Map(spin_start_node,1),S_Map(spin_start_node,2),300,'red.');
    disp(sprintf('Czujnik %d wykryl zagro�enie!',spin_start_node));
    
    if(spin_msg_star(spin_start_node) == 0) % rysuje pierwszy ruch od ktorego przyszla wiadomosci    
        alg_finished = false;
    end;
        
    temp = -1;
    while temp
        temp = S_Map_Sprawdz();        
        if(temp ~= 0)
            if(spin_msg_star(temp) == 0)
                    alg_finished = false;
        	end;      
        end;
    end;
    
    msg_cycle_finished = true;
    
    figure(f1);
    X1=S_Map(:,1);
    Y1=S_Map(:,2);
    Z1=S_Map(:,3);
    plot3(X1,Y1,Z1,'black.');
end

function [ ene ] = S_Map_Sprawdz()
    
    global S_Map;             % Sensors map (see put_sensors.m)
    global nodes_count;
    global Random_Node_List;
    
    ene = 0;    
   
    for li=1:nodes_count
        if(S_Map(Random_Node_List(li),7) == 1 && S_Map(Random_Node_List(li),8)== 0 && S_Map(Random_Node_List(li),3) ~= 0)
            ene = Random_Node_List(li);
        end;
    end;    
end